package com.doublethread.easymart.modules.comment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doublethread.easymart.common.context.UserContext;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.comment.dto.CommentCreateDTO;
import com.doublethread.easymart.modules.comment.dto.CommentQueryDTO;
import com.doublethread.easymart.modules.comment.service.CommentService;
import com.doublethread.easymart.modules.comment.vo.CommentVO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户端评价控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/user/comments")
@RequiredArgsConstructor
public class UserCommentController {

    private final CommentService commentService;

    /**
     * 创建商品评价
     */
    @PostMapping
    public Result<Void> createComment(@Valid @RequestBody CommentCreateDTO commentCreateDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("创建商品评价");

        commentService.createComment(userId, commentCreateDTO);

        log.info("创建商品评价成功");
        return Result.success();
    }

    /**
     * 获取用户评价列表
     */
    @GetMapping
    public Result<Page<CommentVO>> getUserComments(CommentQueryDTO queryDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取用户评价列表");

        Page<CommentVO> commentPage = commentService.getUserComments(userId, queryDTO);

        log.info("获取用户评价列表成功");
        return Result.success(commentPage);
    }

    /**
     * 删除评价
     */
    @DeleteMapping("/{commentId}")
    public Result<Void> deleteComment(@PathVariable Long commentId) {
        Long userId = UserContext.getCurrentUserId();

        log.info("删除评价");

        commentService.deleteComment(userId, commentId);

        log.info("删除评价成功");
        return Result.success();
    }

    /**
     * 检查用户是否已评论某商品
     */
    @GetMapping("/check/{productId}")
    public Result<Boolean> checkUserCommented(@PathVariable Long productId) {
        Long userId = UserContext.getCurrentUserId();

        log.info("检查用户是否已评论商品: userId={}, productId={}", userId, productId);

        boolean hasCommented = commentService.checkUserCommented(userId, productId);

        log.info("用户评论检查结果: userId={}, productId={}, hasCommented={}", userId, productId, hasCommented);
        return Result.success(hasCommented);
    }

    /**
     * 获取订单中商品的评论状态
     */
    @GetMapping("/order/{orderId}/status")
    public Result<Map<Long, Boolean>> getOrderItemsCommentStatus(@PathVariable Long orderId) {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取订单商品评论状态: userId={}, orderId={}", userId, orderId);

        Map<Long, Boolean> statusMap = commentService.getOrderItemsCommentStatus(userId, orderId);

        log.info("订单商品评论状态获取完成: userId={}, orderId={}", userId, orderId);
        return Result.success(statusMap);
    }
}
